using System;

namespace Fornax020.Devices
{
    /// <summary>
    /// X射线设备配置类
    /// 统一管理设备相关的配置参数
    /// </summary>
    public class XRayDeviceConfig
    {
        #region 监控配置
        /// <summary>
        /// 监控定时器间隔（毫秒）
        /// </summary>
        public int MonitoringInterval { get; set; } = 200;
        
        /// <summary>
        /// 监控超时时间（毫秒）
        /// </summary>
        public int MonitoringTimeout { get; set; } = 5000;
        #endregion

        #region KV参数限制
        /// <summary>
        /// 最大KV值限制
        /// </summary>
        public double MaxKvValue { get; set; } = 100.0;
        
        /// <summary>
        /// 最小KV值限制
        /// </summary>
        public double MinKvValue { get; set; } = 0.0;
        
        /// <summary>
        /// KV值步进
        /// </summary>
        public double KvStep { get; set; } = 0.1;
        #endregion

        #region MA参数限制
        /// <summary>
        /// 最大MA值限制
        /// </summary>
        public double MaxMaValue { get; set; } = 500.0;
        
        /// <summary>
        /// 最小MA值限制
        /// </summary>
        public double MinMaValue { get; set; } = 0.0;
        
        /// <summary>
        /// MA值步进
        /// </summary>
        public double MaStep { get; set; } = 1.0;
        #endregion

        #region 连接配置
        /// <summary>
        /// 连接超时时间（毫秒）
        /// </summary>
        public int ConnectionTimeout { get; set; } = 3000;
        
        /// <summary>
        /// 重连间隔（毫秒）
        /// </summary>
        public int ReconnectInterval { get; set; } = 1000;
        
        /// <summary>
        /// 最大重连次数
        /// </summary>
        public int MaxReconnectAttempts { get; set; } = 3;
        #endregion

        #region 安全配置
        /// <summary>
        /// 曝光超时时间（毫秒）
        /// </summary>
        public int ExposureTimeout { get; set; } = 30000;
        
        /// <summary>
        /// 是否启用自动错误恢复
        /// </summary>
        public bool EnableAutoErrorRecovery { get; set; } = true;
        
        /// <summary>
        /// 错误恢复延迟（毫秒）
        /// </summary>
        public int ErrorRecoveryDelay { get; set; } = 2000;
        #endregion

        #region 静态工厂方法
        /// <summary>
        /// 获取默认配置
        /// </summary>
        public static XRayDeviceConfig Default => new XRayDeviceConfig();

        /// <summary>
        /// 获取快速配置（高频率监控）
        /// </summary>
        public static XRayDeviceConfig Fast => new XRayDeviceConfig
        {
            MonitoringInterval = 100,
            MonitoringTimeout = 2000,
            ConnectionTimeout = 1000,
            ReconnectInterval = 500
        };

        /// <summary>
        /// 获取稳定配置（低频率监控）
        /// </summary>
        public static XRayDeviceConfig Stable => new XRayDeviceConfig
        {
            MonitoringInterval = 500,
            MonitoringTimeout = 10000,
            ConnectionTimeout = 5000,
            ReconnectInterval = 2000,
            MaxReconnectAttempts = 5
        };

        /// <summary>
        /// 获取安全配置（严格限制）
        /// </summary>
        public static XRayDeviceConfig Safe => new XRayDeviceConfig
        {
            MaxKvValue = 80.0,
            MaxMaValue = 300.0,
            ExposureTimeout = 15000,
            EnableAutoErrorRecovery = true,
            ErrorRecoveryDelay = 3000
        };
        #endregion

        #region 验证方法
        /// <summary>
        /// 验证KV值是否在有效范围内
        /// </summary>
        /// <param name="kvValue">KV值</param>
        /// <returns>是否有效</returns>
        public bool IsValidKvValue(double kvValue)
        {
            return kvValue >= MinKvValue && kvValue <= MaxKvValue;
        }

        /// <summary>
        /// 验证MA值是否在有效范围内
        /// </summary>
        /// <param name="maValue">MA值</param>
        /// <returns>是否有效</returns>
        public bool IsValidMaValue(double maValue)
        {
            return maValue >= MinMaValue && maValue <= MaxMaValue;
        }

        /// <summary>
        /// 获取KV值的错误描述
        /// </summary>
        /// <param name="kvValue">KV值</param>
        /// <returns>错误描述</returns>
        public string GetKvValueError(double kvValue)
        {
            if (kvValue < MinKvValue)
                return $"KV值不能小于 {MinKvValue}";
            if (kvValue > MaxKvValue)
                return $"KV值不能大于 {MaxKvValue}";
            return string.Empty;
        }

        /// <summary>
        /// 获取MA值的错误描述
        /// </summary>
        /// <param name="maValue">MA值</param>
        /// <returns>错误描述</returns>
        public string GetMaValueError(double maValue)
        {
            if (maValue < MinMaValue)
                return $"MA值不能小于 {MinMaValue}";
            if (maValue > MaxMaValue)
                return $"MA值不能大于 {MaxMaValue}";
            return string.Empty;
        }
        #endregion
    }
}
